<?php


class PaserExcelModel
{
    private $ext;

    private $objExcel;

    private $row;

    private $col;

    private $fieldArr = [];

    private $filePath;

    public function __construct($filePath)
    {
        $this->filePath = $filePath;
        $this->ext = pathinfo($filePath)['extension'];
        $this->objExcel = new \PhpExcel($this->ext);   //  如果是框架里面都会有命名空间，使用方法 -->  new \PhpExcel("xlsx");
        $this->objExcel->LoadFile($filePath);  //  读取刚才生成的excel文件
        $this->objExcel->SheetSwitch(0);  //  切换到第一个sheet（序号从0开始）表准备读取数据
        $this->row = $this->objExcel->GetTotalRow();
        $this->col = $this->objExcel->GetTotalCol();
        setlocale(LC_ALL, array('zh_CN.utf8', 'zh_CN.gbk2312', 'zh_CN.utf-8'));

    }


    /**
     * 读取excel表格字段
     * @param int $addRowIndex 从第几行开始读
     * @param int $addColIndex 从第几列开始读
     */
    private function getFiledArr($RowIndex = 1, $addColIndex = 0)
    {
        for ($i = 0 + $addColIndex; $i < $this->col; $i++) {
            $this->fieldArr[] = $this->readCellData($RowIndex, $i);
        }
    }


    /**
     * 解析excel
     * @param int $RowIndex
     * @param int $addColIndex
     */
    public function paser($RowIndex = 1, $addColIndex = 0)
    {
        $dataArr = [];
        for ($r = 0 + $RowIndex; $r < $this->row; $r++) {
            $row = [];
            for ($c = 0 + $addColIndex; $c < $this->col; $c++) {
                $readStr = $this->readCellData($r, $c);
                $row[$this->fieldArr[$c - $addColIndex]] = $readStr;
            }
            $dataArr[] = $row;
        }
        return $dataArr;
    }

    /**
     * @return mixed
     */
    public function getExt()
    {
        return $this->ext;
    }

    /**
     * @return mixed
     */
    public function getRow()
    {
        return $this->row;
    }

    /**
     * @return mixed
     */
    public function getCol()
    {
        return $this->col;
    }

    /**
     * 读取单元格数据
     * @param $r
     * @param $c
     * @return string
     */
    private function readCellData($r, $c)
    {
        // 首先获取单元格类型（参见后面单元格类型，）
        $cellType = $this->objExcel->GetCellType($r, $c);
        //  我们读取数字或者文本
        if ($cellType == 1 || $cellType == 2 || $cellType == 3) {
            return $this->objExcel->ReadStr($r, $c);   //  千万不要在未判断单元格类型的时候直接读，遇到空白、空值时，本函数会报错
        }
        return '';
    }


    /**
     * 获取excel数组
     * @return array
     */
    public function getDataArr()
    {
        $str1 = $this->readCellData(0, 0); //A1
        $str2 = $this->readCellData(1, 0); //A2

        if (!empty($str1) && $str2 != 'mname') {
            $this->getFiledArr(1, 0); //从第二行开始读
            return ['http://localhost:9636/getArgs', $this->paser(2, 0)];//从第3行开始读
        }
        if ($str2 == 'mname') {
            $this->getFiledArr(1, 0); //从第二行开始读
            return ['http://0.0.0.0:9636/updateAds', $this->paser(2, 0)];//从第3行开始读
        }
        $this->getFiledArr(0, 1); //从b1开始读
        return ['http://localhost:9622/Extension', $this->paser(2, 1)];//从从b2开始读
    }

    public function __destruct()
    {
        @unlink($this->filePath);
    }
}